#!/bin/sh
set -xe

function clean_build_root_exit()
{
    status=$?
    dir=$1
    if [ $status -ne 0 ]; then
        set +e
        umount $dir/proc
        set -e
        rm -rf $dir
    fi
}

function tqe_fortify()
{
    while test $# -gt 0; do
        case $1 in
          *-module)
            MODULE="$2"
            shift
          ;;
          *-make_arg)
            MAKE_ARG="$2"
            shift
          ;;
          *-language)
            LANGUAGE="$2"
            shift
          ;;
          *-make)
             MAKE="make"
          ;;
          *)
            echo "Unknown parameter: $1"
            echo 'Usage: do_tqe_fortify.sh --module $MODULE --make_arg $MAKE_ARG --language $LANGUAGE  (c,j,py,kt,go) --make (以make方式编译) '
            exit 1
          ;;
        esac
          shift
    done

    cd `cat project_name`
    if [ -f last_building -a -f last_tqe_result ]; then
        BUILDROOT=`cat last_building`
        tqe_result=`cat last_tqe_result`
    else
        echo "Failed: there is no last_building or last_tqe_result"
        exit 1
    fi

   # trap "clean_build_root_exit $BUILDROOT" EXIT
    if [ ! -f $BUILDROOT/.build.log ]; then
        echo "Failed:osc build error"
        exit 1
    fi
    rpmbuild_dir=`cat $BUILDROOT/.build.log  | grep '.src.rpm' | awk '{print $NF}' | sed 's/[ :]//g' | awk -F'SRPMS' '{print $1}'`
    PACKAGE_DIR=`ls -F $BUILDROOT/$rpmbuild_dir/BUILD/ | grep '/$'`"$MODULE"

    rm -f $tqe_result/*

    if [ ! -d $BUILDROOT ]; then
        echo "There is no $BUILDROOT"
        exit 1
    fi

    chroot $BUILDROOT sh do_make.sh "$PACKAGE_DIR" "$MAKE_ARG" "$LANGUAGE" "$MAKE"

    last_build_type=`cat last_build_type`
    if [ -n "$(cat tools.txt| grep fortify )" ];then
	    fortify_make_result=`cat $BUILDROOT/fortify_make_result`
	    if [ $fortify_make_result -ne 0  ]; then
		echo "Failed: some error happens on fortify make"
		exit 1
	    fi
    fi
    if [ -n "$(cat tools.txt| grep cov )"  ];then
	    if [ "x$last_build_type"   ==   "x" -o "$last_build_type"   ==   "cplus" -o "$last_build_type"   ==   "sh" ]; then
		tqe_make_result=`cat $BUILDROOT/tqe_make_result`
		if [ $tqe_make_result -ne 0 ]; then
		    echo "Failed: some error happens on tqe make"
		    exit 1
		fi
		if [ -d $BUILDROOT/$rpmbuild_dir/BUILD/$PACKAGE_DIR/cov  ]; then
		    cd $BUILDROOT/$rpmbuild_dir/BUILD/$PACKAGE_DIR/cov
		    zip -9 -r coverity.zip *
		    cp -v coverity.zip $tqe_result
		    cd -
		else
		    echo "Failed: There is no cov dir."
		    exit 1
		fi
	    fi
    fi
    if [ -n "$(cat tools.txt| grep fortify )"  ]; then
	    if [ -d $BUILDROOT/root/.fortify/ ]; then
		cd $BUILDROOT/root/.fortify/
		set +e
		zip -9 -r fortify.zip *
		if [ $? -ne 0 ]; then
		    sleep 30
		    rm -rf fortify.zip
		    zip -9 -r fortify.zip *
		fi
		set -e
		cp -v fortify.zip $tqe_result
		cd -
	    elif [ -d $BUILDROOT/.fortify/  ]; then
		cd $BUILDROOT/.fortify/
		set +e
		zip -9 -r fortify.zip *
		if [ $? -ne 0 ]; then
		    sleep 30
		    rm -rf fortify.zip
		    zip -9 -r fortify.zip *
		fi
		set -e
		cp -v fortify.zip $tqe_result
		cd -
	    else
		echo "Failed: There is no fortify dir."
		exit 1
	    fi
    fi
    if [ -n "$(cat tools.txt| grep CodeMars )"  ]; then
	    if [ -e $BUILDROOT/codemars_Newest/CodeMars.json ]; then
		cd $BUILDROOT/codemars_Newest
		zip -9 -r CodeMars.zip CodeMars.json
		cp -v CodeMars.zip $tqe_result
		cd -
	    else
		echo "Failed: There is no CodeMars json file."
		exit 1
	    fi
    fi
    if [ -n "$(cat tools.txt| grep binscope )"  ]; then
	    if [ -e $BUILDROOT/binscope/binscope.zip  ];then
		cp -v $BUILDROOT/binscope/binscope.zip $tqe_result
	    else
		echo "Failed: There is no binscope.zip in /binscope"
		exit 1
	    fi
    fi


    exit 0
}

tqe_fortify $@
